package com.zhku.zhny.admin.service;

import java.io.File;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.zhku.zhny.admin.constant.ConstantFilePath;
import com.zhku.zhny.admin.dao.AdminDao;
import com.zhku.zhny.admin.dao.AdminDaoImpl;
import com.zhku.zhny.admin.exception.InsertException;
import com.zhku.zhny.admin.exception.RemoveException;
import com.zhku.zhny.admin.exception.SearchException;
import com.zhku.zhny.domain.Admin;
import com.zhku.zhny.domain.Event;
import com.zhku.zhny.domain.EventPageBean;
import com.zhku.zhny.domain.Group;
import com.zhku.zhny.domain.GroupPageBean;
import com.zhku.zhny.domain.Honor;
import com.zhku.zhny.domain.HonorPageBean;
import com.zhku.zhny.domain.Introduction;
import com.zhku.zhny.domain.SignUp;
import com.zhku.zhny.domain.SignUpPageBean;

public class AdminServiceImpl implements AdminService {

	private AdminDao adminDao = new AdminDaoImpl();

	@Override
	public int addGroupInfo(Group group) throws InsertException {
		try {
			return adminDao.insertGroupInfo(group);
		} catch (SQLException e) {
			throw new InsertException("插入失败");
		}
	}

	@Override
	public GroupPageBean findGroupsByName(String groupName, String currentPage, int pageSize) throws SearchException {
		GroupPageBean pageBean = new GroupPageBean();
		try {
			int count = adminDao.countGroupInfo(groupName); // 总记录数
			int totalPage = (int) Math.ceil(count * 1.0 / pageSize); // 总页数
			int currPage = Integer.parseInt(currentPage);
			List<Group> groups = adminDao.selectGroupsByName(groupName, currPage, pageSize);
			pageBean.setCount(count);
			pageBean.setCurrentPage(currPage);
			pageBean.setGroups(groups);
			pageBean.setTotalPage(totalPage);
			pageBean.setPageSize(pageSize);
			return pageBean;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SearchException("查询失败");
		}

	}

	@Override
	public boolean removeGroupById(String groupId) throws RemoveException {
		try {
			String imgPath = adminDao.selectGroupImgPathById(groupId);
			// File file = new File(imgPath);
			File file = new File(ConstantFilePath.LOCALGROUPFILEPATH, imgPath);
			if (file.exists()) {
				file.delete();
			}
			return adminDao.deleteGroupById(groupId);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RemoveException("删除失败！");
		}
	}

	@Override
	public List<Group> findAllGroups() throws SearchException {
		try {
			return adminDao.selectAllGroups();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SearchException("加载失败！");
		}
	}

	@Override
	public int addSignUpInfo(SignUp signUpBean) throws InsertException {
		try {
			return adminDao.insertSignUpInfo(signUpBean);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new InsertException("添加失败！");
		}
	}

	@Override
	public List<String> findGroupsName() throws SearchException {
		try {
			return adminDao.selectGroupsName();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SearchException("查询失败！");
		}
	}

	@Override
	public SignUpPageBean findByNameOrPhone(String name, String phone, String currentPage, int pageSize)
			throws SearchException {
		SignUpPageBean pageBean = new SignUpPageBean();
		try {
			int count = adminDao.countSignUpInfo(name, phone); // 总记录数
			int totalPage = (int) Math.ceil(count * 1.0 / pageSize); // 总页数
			int currPage = Integer.parseInt(currentPage);
			List<SignUp> lists = adminDao.selectSignUp(name, phone, currPage, pageSize);
			pageBean.setCount(count);
			pageBean.setCurrentPage(currPage);
			pageBean.setSignUps(lists);
			pageBean.setTotalPage(totalPage);
			pageBean.setPageSize(pageSize);
			return pageBean;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SearchException("查询失败");
		}
	}

	@Override
	public boolean removeSignUpById(String id) throws RemoveException {
		try {
			return adminDao.deleteSignUpById(id);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RemoveException("删除失败！");
		}
	}

	@Override
	public List<SignUp> findAllSignUp() throws SearchException {
		try {
			return adminDao.selectAllSignUp();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SearchException("查询失败！");
		}
	}

	@Override
	public int addHonorInfo(Honor honor) throws InsertException {
		try {
			return adminDao.insertHonorInfo(honor);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new InsertException("插入失败！");
		}
	}

	@Override
	public HonorPageBean findHonorByNameOrDate(String name, String date, String currentPage, int pageSize)
			throws SearchException {
		HonorPageBean pageBean = new HonorPageBean();
		try {
			int count = adminDao.countHonorInfo(name, date); // 总记录数
			int totalPage = (int) Math.ceil(count * 1.0 / pageSize); // 总页数
			int currPage = Integer.parseInt(currentPage);
			List<Honor> lists = adminDao.selectHonor(name, date, currPage, pageSize);
			pageBean.setCount(count);
			pageBean.setCurrentPage(currPage);
			pageBean.setHonors(lists);
			pageBean.setTotalPage(totalPage);
			pageBean.setPageSize(pageSize);
			return pageBean;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SearchException("查询失败");
		}
	}

	@Override
	public boolean removeHonorById(String honorId) throws RemoveException {
		try {
			String imgPath = adminDao.selectHonorImgPathById(honorId);
			File file = new File(ConstantFilePath.LOCALHONORFILEPATH, imgPath);
			if (file.exists()) {
				file.delete();
			}
			return adminDao.deleteHonorById(honorId);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RemoveException("删除失败！");
		}
	}

	@Override
	public boolean removeSignUpByIds(String[] ids) throws RemoveException {
		try {
			return adminDao.deleteSignUpByIds(ids);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RemoveException("删除失败！");
		}
	}

	@Override
	public boolean removeGroupByIds(String[] ids) throws RemoveException {
		int res = 0;
		try {
			// 循环删除小组信息
			for (int i = 0; i < ids.length; i++) {
				String imgPath = adminDao.selectGroupImgPathById(ids[i]);
				boolean bool = adminDao.deleteGroupById(ids[i]);
				if (bool) {
					File file = new File(ConstantFilePath.LOCALGROUPFILEPATH, imgPath);
					if (file.exists()) {
						file.delete();
					}
					res++;
				}
			}
			if (res == ids.length) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RemoveException("删除失败！");
		}
		return false;
	}

	@Override
	public boolean removeHonorByIds(String[] ids) throws RemoveException {
		try {
			int res = 0;
			// 循环删除荣誉信息
			for (int i = 0; i < ids.length; i++) {
				// 先查后删
				String imgPath = adminDao.selectHonorImgPathById(ids[i]);
				boolean bool = adminDao.deleteHonorById(ids[i]);
				if (bool) {
					res++;
					File file = new File(ConstantFilePath.LOCALHONORFILEPATH, imgPath);
					if (file.exists()) {
						file.delete();
					}
				}
			}
			if (res == ids.length) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RemoveException("删除失败！");
		}
		return false;
	}

	@Override
	public List<SignUp> findSignUpsByIds(String[] ids) throws SearchException {
		List<SignUp> list = new LinkedList<SignUp>();
		SignUp signUp = null;
		try {
			for (int i = 0; i < ids.length; i++) {
				signUp = adminDao.selectSignUpById(ids[i]);
				list.add(signUp);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SearchException("查询失败！");
		}
	}

	@Override
	public SignUp findSignUpsById(String id) throws SearchException {
		try {
			return adminDao.selectSignUpById(id);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SearchException("查询失败！");
		}
	}

	@Override
	public List<Honor> findAllHonor() throws SearchException {
		try {
			return adminDao.selectAllHonor();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SearchException("查询失败！");
		}
	}

	@Override
	public Admin login(String name, String password) throws SearchException {
		try {
			return adminDao.selectAdmin(name, password);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SearchException("查询失败！");
		}
	}

	@Override
	public int addEventInfo(Event event) throws InsertException {
		try {
			return adminDao.insertEventInfo(event);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new InsertException("插入失败!");
		}
	}

	@Override
	public int addIntroductionInfo(Introduction intro) throws InsertException {
		try {
			return adminDao.insertIntroductionInfo(intro);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new InsertException("插入失败!");
		}
	}

	@Override
	public List<Introduction> findAllIntros() throws SearchException {
		try {
			return adminDao.selectAllIntroduction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SearchException("查询失败！");
		}
	}

	@Override
	public boolean removeIntroByIds(String[] ids) throws RemoveException {
		String imgPath = "";
		try {
			// 删除单个
			if (ids.length == 1) {
				imgPath = adminDao.selectIntroImgPathById(ids[0]);
				boolean res = adminDao.deleteIntroById(ids[0]);
				if(res) {
					File file = new File(ConstantFilePath.LOCALINTROFILEPATH, imgPath);
					if (file.exists()) {
						file.delete();
					}
					return true;
				}else {
					return false;
				}
			} else {
				int res = 0;
				// 循环删除团队介绍信息
				for (int i = 0; i < ids.length; i++) {
					imgPath = adminDao.selectIntroImgPathById(ids[i]);
					boolean bool = adminDao.deleteIntroById(ids[i]);
					if (bool) {
						res++;
						File file = new File(ConstantFilePath.LOCALINTROFILEPATH, imgPath);
						if (file.exists()) {
							file.delete();
						}
					}
				}
				if (res == ids.length) {
					return true;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RemoveException("删除失败！");
		}
		return false;
	}

	@Override
	public List<Event> findAllEvent() throws SearchException {
 		try {
			return adminDao.selectAllEvent();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SearchException("查询失败!");
		}
	}

	@Override
	public boolean removeEventByIds(String[] ids) throws RemoveException {
		String imgPath = "";
		try {
			// 删除单个
			if (ids.length == 1) {
				imgPath = adminDao.selectEventImgPathById(ids[0]);
				boolean res = adminDao.deleteEventById(ids[0]);
				if(res) {
					File file = new File(ConstantFilePath.LOCALEVENTFILEPATH, imgPath);
					if (file.exists()) {
						file.delete();
					}
					return true;
				}else {
					return false;
				}
			} else {
				int res = 0;
				// 循环删除团队介绍信息
				for (int i = 0; i < ids.length; i++) {
					imgPath = adminDao.selectEventImgPathById(ids[i]);
					boolean bool = adminDao.deleteEventById(ids[i]);
					if (bool) {
						res++;
						File file = new File(ConstantFilePath.LOCALEVENTFILEPATH, imgPath);
						if (file.exists()) {
							file.delete();
						}
					}
				}
				if (res == ids.length) {
					return true;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RemoveException("删除失败！");
		}
		return false;
	}

	@Override
	public EventPageBean findByTitleOrDate(String title, String date, String currentPage, int pageSize)
			throws SearchException {
		EventPageBean pageBean = new EventPageBean();
		try {
			int count = adminDao.countEventInfo(title, date); // 总记录数
			int totalPage = (int) Math.ceil(count * 1.0 / pageSize); // 总页数
			int currPage = Integer.parseInt(currentPage);
			List<Event> list = adminDao.selectEvent(title, date, currPage, pageSize);
			pageBean.setCount(count);
			pageBean.setCurrentPage(currPage);
			pageBean.setEvents(list);
			pageBean.setTotalPage(totalPage);
			pageBean.setPageSize(pageSize);
			return pageBean;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SearchException("查询失败");
		}
	}

}
